$(document).ready(function () {
    //打开页面的时候执行
    //   initSelect();

    validateRule();

    var totalPage = initSelect(1);
    initPage(totalPage);

    //给删除按钮添加响应事件
    $("#scoreTable").on("click","tbody>tr .scoreDelBtn",function() {
        //alert(JSON.stringify($(this)));
        var trSeq = $(this).parents().filter("tbody").find(".scoreDelBtn").index($(this)[0]); //所获取的当前行的行号
        var input_val = $("#scoreTableBody tr:eq("+trSeq+")").find("td:first").text();
        alert("当前删除操作，行号为: " + trSeq + "内容为:" +input_val);

        // var val = $(this).find("td:first").text(); //所获取的当前行的行号
        // alert("内容为:" + val);
        deletescoreById(input_val);

    });

    //给修改按钮添加响应事件
    $("#scoreTable").on("click","tbody>tr .scoreUpdateBtn",function() {
        //获取单行信息
        var trSeq = $(this).parents().filter("tbody").find(".scoreUpdateBtn").index($(this)[0]); //所获取的当前行的行号
        var tr = $("#scoreTableBody tr:eq("+trSeq+")");
        var id = tr.find("td:first").text();
        var stuName = tr.find("td:nth-child(2)").text();
        var stuScore = tr.find("td:nth-child(3)").text();
        //封装成对象进行传递
        var data = {};
        data.id = id;
        data.stuName = stuName;
        data.userScore = stuScore;
        //调起修改弹窗！
        changeDialog(data);
    });


});








// $.validator.setDefaults({
//     submitHandler : function() {
//
//             scoreAdd();
//
//             modifyscore()
//
//     }
// });

function validateRule() {
    $("#scoreForm").validate({
        rules : {
            stuName : {
                required : true,

            },

            stuScore:{
                required : true
            }


        },
        messages : {
            stuName : {
                required : "请输入学生姓名",

            },

            stuScore : {
                required : "分数呢？"
            }
        }
    });

    $("#ScoreAddBtn").click(function(){
        if($("#scoreForm").valid()){
            scoreAdd();
        }
    });



    $("#scoreChangeForm").validate({
        rules : {
            stuName : {
                required : true,

            },


            stuScore:{
                required : true
            }


        },
        messages : {
            stuName : {
                required : "请输入学生姓名",

            },

            stuScore : {
                required : "分数呢？"
            }
        }
    });


    $("#scoreUpdateBtn").click(function(){
        if($("#scoreChangeForm").valid()){
            modifyscore();
        }
    });


}

function scoreAdd(){
    var data = {};
    data.userRealname = $("#stuName").val();
    data.courseName = localStorage.getItem("courseName");
    data.userScore = $("#stuScore").val();

    alert(JSON.stringify(data));


    $.ajax({

        type:"POST",
        url:"/score/scoreadd",
        data:JSON.stringify(data),
        contentType:'application/json',
        dataType:"json",
        success:function (data) {
            if(data.code === 1000001){
                alert("分数添加成功");
                window.location.reload();
            }else{
                alert(data.msg);
            }
        }
    })
}

function deletescoreById(id) {
    var data = {};
    data.scoreId = id;
    alert(JSON.stringify(data));
    var r=confirm("确认要删除嘛？")
    if (r !==true){
        return;
    }
    $.ajax({
        type:'POST',
        url:'/score/delScore',
        data: id,
        contentType:'application/json',
        dataType:'json',
        success: function (data) {
            if (data.code= 100001){
                window.location.reload();
            } else {
                alert(data.msg)
            }
        }

    });

}


function initSelect(number) {
    var data = {};
    data.sessionId = localStorage.getItem("token_session");
    data.courseName = localStorage.getItem("courseName")
    data.pageNo = number;
    data.pageSize = 10;
    var totalPage;
    $.ajax({
        type: 'POST',
        url: '/score/findScoreBPAC',
        data: JSON.stringify(data),
        contentType:'application/json',
        dataType: 'json',
        async:false,
        success: function (data) {
            console.log(JSON.stringify(data));
            // alert(JSON.stringify(data.data));
            var table = $("#scoreTableBody");
            table.empty();
            // alert(data.data);
            $.each(data.data, function (n, value) {


                var tr =
                    "<tr>" +
                     "<td>"+value.scoreId+"</td>"+
                    "<td>"+value.userRealname+"</td>" +
                    "<td>"+value.userScore+"</td>" +

                    "<td><div class='hidden-sm hidden-xs btn-group'> " +
                    "<button class='btn btn btn-xs btn-info scoreUpdateBtn'> <a href='#change-table' role='button' class='white ace-icon fa fa-pencil bigger-120' data-toggle='modal'></a> </button>"+
                    "<button class='btn btn-xs btn-danger scoreDelBtn'><i class='ace-icon fa fa-trash-o bigger-120'></i></button>" +
                    "</div> </td>" +
                    " </tr>";
                table.append(tr);
                 $('#scoreTable tr').find('td:eq(0)').hide();
            });
            totalPage = data.totalPage;
        }

    });
    return totalPage;
}


function changeDialog(data) {
    alert(JSON.stringify(data));
    $("#scoreId").val(data.id);
    $("#stuName1").val(data.stuName);
    $("#stuScore1").val(data.userScore);
}

function modifyscore() {

    var data = {};
    data.scoreId = $("#scoreId").val();
    data.userRealname = $("#stuName1").val();
    data.courseName = localStorage.getItem("courseName");
    data.userScore = $("#stuScore1").val();

    $.ajax({
        type: 'POST',
        url: '/score/scoreUpdate',
        data:JSON.stringify(data),
        contentType:'application/json',
        dataType:"json",
        success: function (data) {
            if(data.code === 1000001){
                alert("信息修改成功！");
                window.location.reload();
            }else{
                alert(data.msg);
            }
        }

    });
}



function initPage(totalPage) {
    $(".simplePaging7").simplePaging({
        allPage: totalPage,//总页数
        showPage: 10,//显示页数
        startPage: 1,//第一页页码数字
        initPage: 1,//加载完毕自动跳转到第n页
        first: "首页",//首页显示字符
        last: "尾页",//尾页显示字符
        prev: "«",//上一页显示字符
        next: "»",//下一页显示字符
        showTurnTo: false,//是否显示跳转按钮，false不显示，true显示
        animateType: "normal",//过渡模式：动画“animation”、跳动“jumpy”、快速移动“fast”、正常速度移动“normal”、缓慢的速度移动“slow”、异常缓慢的速度移动“verySlow”
        animationTime: 300,//animateType为animation时，动画过渡时间(ms)
        callBack: function (num) {
            initSelect(num);
        }
    });
}
